<?php


/*
 * @version :  2013-9-22
 * @project :  account-system
 * @Author  :  Allen
 * @e-mail  :  shulei@maszy.cn
 */
/**
*  代码0399 第三方登陆成功
*  代码0398 第三方注册成功
*  代码0397 第三方媒体id存在 存在该用户user_id
*  代码0396 使用mac地址登陆成功
*  代码0395 使用username和password登陆成功
* 
*  代码0300	 第三方登陆失败,第三方系统ID不存在
*  代码0301	 mac登陆失败,mac地址不存在
*  代码0302 使用username和password登陆失败,账号或密码错误
*/

//第三方登陆
function login_by_third() {
	$third_api_id = $_REQUEST['third_api_id'];
	$way = $_REQUEST['way']; //第三方登陆用的哪种方式
	$sql = "select * from zy_third_api where third_api_id='$third_api_id'";
	$rs = mysql_query($sql);
	if ($row = mysql_fetch_array($rs)) //找到该用户第三方登陆的记录  根据传过来的third_api_id找到该用户并将输入的信息进行修改
		{
		$queryuserId = $row['user_id'];
		return array (
			'message' => '0399',
			'user_id' => $queryuserId
		); //第三方登陆成功
	}
	/*else{//没有找到该用户第三方登陆的记录   返回值0
		
		return array('message'=>'0');//第三方登陆成功
	}*/
	return array (
		'message' => '0300'
	); //第三方登陆失败
}
//第三方注册 
function register_by_third($user_id, $third_api_id, $way) {
	//根据user_id 及 third_api_id 进行第三方绑定.
	$time = date('Y-m-d H:i:s');
	$sql = "insert into `zy_third_api`(`user_id`,`third_api_id`,`type`,`create_time`) values($user_id,'$third_api_id','$way','$time')";
	mysql_query($sql);

}
//根据mac地址进行登陆
function showuseridBymacaddress($mac_address) {
	$sqlmac = "select * from zy_equipment  where MacAddress='" . $mac_address . "'";
	$result = mysql_query($sqlmac);
	if ($row = mysql_fetch_array($result)) {
		return array (
			'message' => '0396',
			'user_id' => $row['updater']
		); //登陆成功
	}

	return array (
		'message' => '0301'
	); //mac登陆失败
}
//根据用户名密码进行登陆
function showuseridByUsernameAndPassword($username, $password) {
	$sql = "select * from zy_user where user_name = '" . $username . "' and password = '" . $password . "'";
	$result = mysql_query($sql);
	if ($row = mysql_fetch_array($result)) {
		return array (
			'message' => '0395',
			'user_id' => $row['user_id']
		); //登陆成功
	}
	return array (
		'message' => '0302'
	); //账号密码登陆失败
}
//普通用户注册(这里验证不需要做的非常细)
function register_user_php($user, $user_name_type) {
	//填写用户相关信息 
	$first_name = $user['first_name'];
	$last_name = $user['last_name'];
	$email = $user['email'];
	$invite_code = $user['invite_code'];
	$invite_user_id = $user['invite_user_id'] > 0 ? $user['invite_user_id'] : 0;
	$mobile = $user['mobile'];
	$password = $user['password'];
	$third_api_id = $user['third_api_id'];
	$way = $user['way'];
	if ($user_name_type == "mobile") {
		$user['user_name'] = $user['mobile'];
	}
	elseif ($user_name_type == "email") {
		$user['user_name'] = $user['email'];
	} else {
		$user['user_name'] = $user['mobile'];
	}
	if(isset($user['third_api_id']))
	{
		$first_name=$user['first_name'];
		$last_name=$user['last_name'];
		$last_name=$user['email'];
		$invite_code=$user['invite_code'];
		$invite_user_id=$user['invite_user_id']>0?$user['invite_user_id']:0;
		$mobile=$user['mobile'];
		$password=$user['password'];
		$third_api_id=$user['third_api_id'];
		$way=$user['way'];
		$sql="select * from zy_third_api where third_api_id='$third_api_id'";
		$rs=mysql_query($sql);
		if($row=mysql_fetch_array($rs))
		{
			return array('message'=>'0397','user_id'=>$row['user_id']);//存在该用户的第三方登陆的记录
		}
	}
	/*判断用户名是否存在*/
	$sql = "select user_name from zy_user where user_name='" . $user['user_name'] . "' and level=2";
	$rs = mysql_query($sql);
	if ($row = mysql_fetch_array($rs)) {
		return array (
			"message" => "0101"
		);
	}
	if ($mobile != "") {
		/*判断手机号是否存在*/
		$sql = "select telephone from zy_user where telephone='" . $mobile . "' and level=2";
		$rs = mysql_query($sql);
		if ($row = mysql_fetch_array($rs)) {
			return array (
				"message" => "0105"
			);
		}else{
			if($user_name_type=="mobile"){
				$user['user_name']=$user['mobile'];
			}else if($user_name_type=="email"){
				$user['user_name']=$user['email'];
			}
			$sql = "INSERT INTO `zy_user`(`user_name`,`password`,`level`,`first_name`,`last_name`,`telephone`,`email`,`invite_code`,`invite_user_id`,reg_time) VALUES ('".$user['user_name']."','$password',2,'$first_name','$last_name','$mobile','$email','$invite_code',$invite_user_id,now())";
			$rs=mysql_query($sql);	
			$user_id=mysql_insert_id();  //返回自增的user_id;
			register_by_third($user_id,$third_api_id,$way);
			return array('message'=>'0398','user_id'=>$user_id);//注册成功
		}
	}
	if ($email != "") {
		/*判断邮箱是否存在*/
		$sql = "select email from zy_user where email='" . $email . "' and level=2";
		$rs = mysql_query($sql);
		if ($row = mysql_fetch_array($rs)) {
			return array (
				"message" => "0109"
			);
		}
	}

	$sql = "INSERT INTO `zy_user`(`user_name`,`password`,`level`,`first_name`,`last_name`,`telephone`,`email`,`invite_code`,`invite_user_id`,reg_time) VALUES ('" . $user['user_name'] . "','$password',2,'$first_name','$last_name','$mobile','$email','$invite_code',$invite_user_id,now())";
	$rs = mysql_query($sql);
	$user_id = mysql_insert_id(); //返回自增的user_id;
	register_by_third($user_id, $third_api_id, $way);
	return array (
		'message' => '0398',
		'user_id' => $user_id
	); //注册成功
}
?>
